package cn.com.wxd.wechat.company.util.xml;

import cn.com.wxd.wechat.company.bean.WxCpXmlMessage;
import cn.com.wxd.wechat.company.bean.WxCpXmlOutImageMessage;
import cn.com.wxd.wechat.company.bean.WxCpXmlOutMessage;
import cn.com.wxd.wechat.company.bean.WxCpXmlOutNewsMessage;
import cn.com.wxd.wechat.company.bean.WxCpXmlOutTextMessage;
import cn.com.wxd.wechat.company.bean.WxCpXmlOutVideoMessage;
import cn.com.wxd.wechat.company.bean.WxCpXmlOutVoiceMessage;
import cn.com.wxd.wechat.common.util.xml.XStreamInit;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.thoughtworks.xstream.XStream;

public class XStreamTransformer {

	@SuppressWarnings("rawtypes")
	protected static final Map<Class, XStream> CLASS_2_XSTREAM_INSTANCE = configXStreamInstance();

	/**
	 * xml -> pojo
	 * @param clazz
	 * @param xml
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T fromXml(Class<T> clazz, String xml) {
		T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(xml);
		return object;
	}

	@SuppressWarnings("unchecked")
	public static <T> T fromXml(Class<T> clazz, InputStream is) {
		T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(is);
		return object;
	}

	/**
	 * pojo -> xml
	 * @param clazz
	 * @param object
	 * @return
	 */
	public static <T> String toXml(Class<T> clazz, T object) {
		return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);
	}

	@SuppressWarnings("rawtypes")
	private static Map<Class, XStream> configXStreamInstance() {
		Map<Class, XStream> map = new HashMap<Class, XStream>();
		map.put(WxCpXmlMessage.class, config_WxCpXmlMessage());
		map.put(WxCpXmlOutNewsMessage.class, config_WxCpXmlOutNewsMessage());
		map.put(WxCpXmlOutTextMessage.class, config_WxCpXmlOutTextMessage());
		map.put(WxCpXmlOutImageMessage.class, config_WxCpXmlOutImageMessage());
		map.put(WxCpXmlOutVideoMessage.class, config_WxCpXmlOutVideoMessage());
		map.put(WxCpXmlOutVoiceMessage.class, config_WxCpXmlOutVoiceMessage());
		return map;
	}

	private static XStream config_WxCpXmlMessage() {
		XStream xstream = XStreamInit.getInstance();
		xstream.processAnnotations(WxCpXmlMessage.class);
		xstream.processAnnotations(WxCpXmlMessage.ScanCodeInfo.class);
		xstream.processAnnotations(WxCpXmlMessage.SendPicsInfo.class);
		xstream.processAnnotations(WxCpXmlMessage.SendPicsInfo.Item.class);
		xstream.processAnnotations(WxCpXmlMessage.SendLocationInfo.class);
		return xstream;
	}

	private static XStream config_WxCpXmlOutImageMessage() {
		XStream xstream = XStreamInit.getInstance();
		xstream.processAnnotations(WxCpXmlOutMessage.class);
		xstream.processAnnotations(WxCpXmlOutImageMessage.class);
		return xstream;
	}

	private static XStream config_WxCpXmlOutNewsMessage() {
		XStream xstream = XStreamInit.getInstance();
		xstream.processAnnotations(WxCpXmlOutMessage.class);
		xstream.processAnnotations(WxCpXmlOutNewsMessage.class);
		xstream.processAnnotations(WxCpXmlOutNewsMessage.Item.class);
		return xstream;
	}

	private static XStream config_WxCpXmlOutTextMessage() {
		XStream xstream = XStreamInit.getInstance();
		xstream.processAnnotations(WxCpXmlOutMessage.class);
		xstream.processAnnotations(WxCpXmlOutTextMessage.class);
		return xstream;
	}

	private static XStream config_WxCpXmlOutVideoMessage() {
		XStream xstream = XStreamInit.getInstance();
		xstream.processAnnotations(WxCpXmlOutMessage.class);
		xstream.processAnnotations(WxCpXmlOutVideoMessage.class);
		xstream.processAnnotations(WxCpXmlOutVideoMessage.Video.class);
		return xstream;
	}

	private static XStream config_WxCpXmlOutVoiceMessage() {
		XStream xstream = XStreamInit.getInstance();
		xstream.processAnnotations(WxCpXmlOutMessage.class);
		xstream.processAnnotations(WxCpXmlOutVoiceMessage.class);
		return xstream;
	}

}
